//
//  UIView+Base.m
//  FashionSpring
//
//  Created by 吕飞俊 on 15/6/27.
//  Copyright (c) 2015年 润媛. All rights reserved.
//

#import <objc/runtime.h>
#import "UIView+lvfjHelper.h"
#import "UIView+lvfjFrame.h"
#import "lvfjCommon.h"
#import "UIColor+lvfjHelper.h"

@implementation UIView (lvfjHelper)
//圆角外发光:外层用圆角透明,内层用阴影底色
-(void)addAction:(SEL)action{
    UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:dismissTap];
}

#pragma mark TopBt
-(UIButton *)crateTopBt{
    UIButton *topBt =[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth * 0.8, ScreenHeight * 0.5, 40, 40)];
    [topBt setBackgroundImage:[UIImage imageNamed:@"toTop.jpg"] forState:UIControlStateNormal];
    topBt.hidden = YES;
    topBt.backgroundColor = [UIColor clearColor];
    [self addSubview:topBt];
    return topBt;
}

-(void)toTop:(UIScrollView *)scrollView{
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0) animated:YES];
}

-(void)scrollViewEndScroll:(UIScrollView *)scrollView setTopBtHidden:(UIButton *)bt{
    if (scrollView.contentOffset.y > 480) {
        bt.hidden = NO;
    }else{
        bt.hidden = YES;
    }
}

#pragma mark setArrayObject
-(void)setArrayObject:(NSArray *)array{
    objc_setAssociatedObject(self, "array", array, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSArray *)getArrayObject{
    return objc_getAssociatedObject(self, "array");
}

#pragma mark layer

// CornerRadius

-(void)setRound{
    [self setCornerRadiusSize:self.width / 2];
}

-(void)setCornerNext{
    [self setCornerRadiusSize:3];
}

-(void)setCommonCornerRadius{
    [self setCornerRadiusSize:15/3/2];
}

-(void)setCornerRadiusSize:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}

-(void)setCommonCornerRadiusAndBorder{
    [self setBorderWidth:1 borderColor:[UIColor colorWith0xRGB:0xFFB933] cornerRadius:3];
}

-(void)setBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor cornerRadius:(CGFloat)cornerRadius{
    [self setBorderWidth:borderWidth borderColor:borderColor];
    [self setCornerRadiusSize:cornerRadius];
}

// borderWidth

-(void)setBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = borderColor.CGColor;
}

-(void)setShadowOffset:(CGSize)shadowOffset
         shadowOpacity:(CGFloat)shadowOpacity{
    self.layer.shadowOffset = shadowOffset;
    self.layer.shadowOpacity = shadowOpacity;
    
}

-(void)setShadowOffset:(CGSize)shadowOffset
         shadowOpacity:(CGFloat)shadowOpacity
          shadowRadius:(CGFloat)shadowRadius{
    [self setShadowOffset:shadowOffset
            shadowOpacity:shadowOpacity];
    self.layer.shadowRadius = shadowRadius;
}

#pragma markget xib

+(UIView *)getNibViewWithNibName:(NSString *)nibName
                           index:(NSInteger)index{
    UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    NSArray *viewArray = [nib instantiateWithOwner:self options:nil];
    id view = [viewArray objectAtIndex:index];
    return view;
}

+(UIView *)getNibViewFirstWithNibName:(NSString *)nibName{
    return [UIView getNibViewWithNibName:nibName index:0];
}

@end
